package com.exp.security.sys.controller;

import com.exp.security.config.DBUserDetailsManager;
import com.exp.security.sys.model.User;
import com.exp.security.sys.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author exp
 * @since 2025-01-03
 */
@RestController
@RequestMapping("/sys/user")
public class UserController {

    @Resource
    private IUserService userService;

    @Resource
    private DBUserDetailsManager dbUserDetailsManager;

    @RequestMapping("/list")
    public List<User> queryUserList() {
        return userService.list();
    }

    @PostMapping("/save")
    public void saveUser(@RequestBody User user) {
        // 参考基于内存的用户配置来创建 UserDetails 实例
        UserDetails userDetails = org.springframework.security.core.userdetails.User
                .withDefaultPasswordEncoder()
                .username(user.getUserName())
                .password(user.getPassword())
                .roles("USER").build();
        dbUserDetailsManager.createUser(userDetails);
    }

}
